

/* CREATE TEMPORARY FILES OF TOP FEMALE SHARE FOR FIRMS IN MATCHED DATA */


// The codes comment out here are used for generate the intermediate input data. Users should run them the first time and then leave them commented out. 


/*
use "${dataout}turnover_allyears_violentfirms", clear
keep sykstun year 
duplicates drop 
save "${dataout}violentfirms_id_year", replace 


use "${dataout}total_plant_employee_allyears", replace
drop if sykstun=="" 
merge m:1 sykstun year using "${dataout}violentfirms_id_year"
keep if _merge==3
drop _merge

**** generate firm characteristics for all firms 
gen employee=1
gen earnings=tyotu
replace earnings=0 if earnings==.
gen female_employee=(sukup=="2")


*keep female_employee earnings sykstun year

*** Create female share of top earners in the firm 
bys sykstun year: gen total_employee = _N
g thresh = floor(0.9*total_employee)

sort year sykstun earnings
by year sykstun : gen e_rank = _n
g top_earner = (e_rank > thresh)
g female_top_earner = female_employee*top_earner
bys year sykstun : egen total_female_top = total(female_top_earner)
g share_female_top = total_female_top/(total_employee - thresh) 

collapse (mean) share_female_top, by(sykstun year)

ren year year_event
save "${dataout}total_plant_topfemale", replace 
*/
/*================================================================

				Regression analysis for firm outcomes, violent firms 

/=================================================================*/

*log using "${logfiles}f06_turnover_match_causal.log", replace

sysdir set PERSONAL "W:\ado"
set scheme kailascheme


*sysdir set PERSONAL "W:\ado"

*Defome dummie used in the eventstudies
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
*define fixed effects 
global fe = "baseid time year" 
*define clustering
global cluster = "sykstun"


capture program drop eventStudyGraphs
program define eventStudyGraphs
	args a b c d
	
	quietly {
	
	preserve
	gen t = _n
	replace t = t-11 
	replace t = . if t > 5
	
	gen coef_est =. 
	gen se_est = . 

	
	
	noisily:  reghdfe `a' $dummies if female_firm==`d', absorb($fe )  cluster($cluster)	
	
	
	*Store coef_estficients
	forvalues i= 0(1)5 {
		cap	replace coef_est= _b[dpl_`i']  if t == -`i'
		cap	replace se_est =  _se[dpl_`i']  if t == -`i'
	}
		
	forvalues i= 1(1)5 {
		cap	replace coef_est = _b[dpl`i']  if t == `i'
		cap	replace se_est =  _se[dpl`i']  if t == `i'
	}

	replace coef_est = 0 if t == - 1
	replace se_est = 0 if t == -1
	replace t = . if missing(coef_est)

	gen uCi = coef_est + se_est*1.96
	gen lCi  = coef_est - se_est*1.96
	


	
	*Main regression for top guys 
	reghdfe `a'  treatPost if female_firm==`d',  absorb($fe)  cluster($cluster)
	local beta = string(_b[treatPost], "%10.3fc")
	local se = string(_se[treatPost], "%10.3fc")
	gen obs = e(N)
		
	twoway 		(rarea uCi lCi t ,color(gs10%50) lwidth(none) )  ///
		(connected coef_est t, msymbol(O)  lcolor(gs2) mcolor(gs2) lpattern(longdash_dot)  xlabel(-5 (1) 5) ylab(`b')  ///
	     yline(0, lpattern(dash) lcolor(black)) xline(0, lpattern(dash) lcolor(black)) ytitle(`c') xtitle("Time since Violence") ), ///
		 legend( order( 2 "Violent firms" ) rows(2) position(7) ring(0) ) ///
			
	gr export "${results}\reg_`a'_fem_managers`d'.pdf", replace
	gr export "${results}\reg_`a'_fem_managers`d'.eps", replace
	
	* Save estimates 
	keep coef* se_est uC* lC* obs* t
	drop if missing(t)
	gen outcome = "`a'"

	restore
	}
end

use "${dataout}turnover_allyears_violentfirms", clear

sort match_id1 year_event time sykstun
bys match_id1 year_event time: ereplace mm=max(mm)
bys match_id1 year_event time: ereplace mf=max(mf)
bys match_id1 year_event time: ereplace new_wp_crime=max(new_wp_crime)
keep if new_wp_crime==1


merge m:1 sykstun year_event using  "${dataout}total_plant_topfemale"
drop if _merge ==2
drop _merge



gen baseyr=year_event

assert time >= -5 & time <= 5 

gen treat= wp_crime==1

*gen age_time=age+time


*Time displacement dummies
g dpl_5=time==-5 & treat==1
g dpl_4=time==-4 & treat==1
g dpl_3=time==-3 & treat==1
g dpl_2=time==-2 & treat==1
g dpl_1=time==-1 & treat==1
g dpl_0=time==0 & treat==1
gen dpl1=time==1 & treat==1
gen dpl2=time==2 & treat==1
gen dpl3=time==3 & treat==1
gen dpl4=time==4 & treat==1
gen dpl5=time==5 & treat==1

gen treatPost= treat==1 & time>0

* share heterogeneity 
su share_female_top, detail
local med = r(p50)

g female_firm = (share_female_top >= `med')


* Generate treatment wave indentifier for each individual
egen baseid=group(sykstun baseyr)


preserve 
keep if mf==1 
eventStudyGraphs "share_female" "-0.1(0.02)0.04" "Share of female employees" "0"
eventStudyGraphs "share_female" "-0.1(0.02)0.04" "Share of female employees" "1"
restore 